Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 562)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.94931 12.94969 12.95009 12.95049 12.95090 12.95131 12.95172 12.95211
##   [9] 12.95250 12.95286 12.95320 12.95352 12.95380 12.95404 12.95424 12.95440
##  [17] 12.95450 12.95455 12.95453 12.95445 12.95430 12.95407 12.95377 12.95338
##  [25] 12.95290 12.95232 12.95165 12.95087 12.94999 12.94899 12.94789 12.94669
##  [33] 12.94540 12.94402 12.94256 12.94104 12.93945 12.93780 12.93610 12.93436
##  [41] 12.93259 12.93079 12.92896 12.92712 12.92527 12.92342 12.92158 12.91975
##  [49] 12.91794 12.91615 12.91441 12.91270 12.91104 12.90943 12.90789 12.90641
##  [57] 12.90502 12.90351 12.90172 12.89965 12.89731 12.89472 12.89189 12.88883
##  [65] 12.88554 12.88205 12.87836 12.87449 12.87044 12.86623 12.86187 12.85737
##  [73] 12.85274 12.84799 12.84314 12.83819 12.83316 12.82806 12.82289 12.81768
##  [81] 12.81243 12.80716 12.80187 12.79658 12.79129 12.78603 12.78079 12.77560
##  [89] 12.77046 12.76539 12.76040 12.75549 12.75068 12.74598 12.74140 12.73696
##  [97] 12.73266 12.72852 12.72454 12.72075 12.71714 12.71374 12.71055 12.70758
## [105] 12.70485 12.70237 12.70015 12.69819 12.69652 12.69514 12.69407 12.69331
## [113] 12.69288 12.69275 12.69290 12.69332 12.69401 12.69496 12.69619 12.69767
## [121] 12.69942 12.70144 12.70370 12.70623 12.70901 12.71204 12.71532 12.71885
## [129] 12.72263 12.72665 12.73092 12.73542 12.74016 12.74514 12.75036 12.75581
## [137] 12.76148 12.76739 12.77353 12.77988 12.78799 12.79905 12.81261 12.82820
## [145] 12.84539 12.86369 12.88265 12.90182 12.92074 12.93894 12.95597 12.97137
## [153] 12.98468 12.99544 13.00549 13.01691 13.02959 13.04345 13.05837 13.07427
## [161] 13.09103 13.10857 13.12678 13.14556 13.16482 13.18445 13.20436 13.22444
## [169] 13.24460 13.26474 13.28476 13.30455 13.32403 13.34308 13.36162 13.37954
## [177] 13.39674 13.41313 13.42860 13.44305 13.45639 13.46851 13.47933 13.48873
## [185] 13.49662 13.50290 13.50746 13.51022 13.51107 13.51043 13.50880 13.50621
## [193] 13.50268 13.49825 13.49294 13.48678 13.47979 13.47201 13.46347 13.45419
## [201] 13.44419 13.43351 13.42218 13.41022 13.39766 13.38453 13.37085 13.35666
## [209] 13.34198 13.32684 13.30940 13.28816 13.26366 13.23644 13.20707 13.17607
## [217] 13.14400 13.11140 13.07882 13.04680 13.01590 12.98664 12.95959 12.93528
## [225] 12.91121 12.88464 12.85577 12.82482 12.79200 12.75751 12.72156 12.68436
## [233] 12.64613 12.60706 12.56737 12.52726 12.48695 12.44663 12.40653 12.36685
## [241] 12.32780 12.28959 12.25242 12.21650 12.18204 12.14926 12.11836 12.08954
## [249] 12.06302 12.03901 12.01771 11.99933 11.98394 11.97126 11.96097 11.95279
## [257] 11.94640 11.94151 11.93780 11.93499 11.93276 11.93080 11.92883 11.92653
## [265] 11.92361 11.91975 11.91466 11.90803 11.90255 11.90071 11.90192 11.90560
## [273] 11.91115 11.91799 11.92553 11.93318 11.94036 11.94647 11.95093 11.95315
## [281] 11.95254 11.94851 11.94292 11.93794 11.93354 11.92965 11.92621 11.92317
## [289] 11.92046 11.91803 11.91582 11.91378 11.91183 11.90993 11.90802 11.90604
## [297] 11.90393 11.90164 11.89910 11.89625 11.89305 11.88942 11.88532 11.88068
## [305] 11.87545 11.86956 11.86297 11.85561 11.84742 11.83834 11.82692 11.81199
## [313] 11.79396 11.77324 11.75025 11.72539 11.69907 11.67171 11.64371 11.61549
## [321] 11.58746 11.56001 11.53358 11.50856 11.48536 11.46440 11.44609 11.43084
## [329] 11.41905 11.40733 11.39242 11.37498 11.35564 11.33504 11.31383 11.29264
## [337] 11.27212 11.25291 11.23565 11.22098 11.20954 11.20198 11.19893 11.19928
## [345] 11.20142 11.20528 11.21079 11.21787 11.22644 11.23644 11.24779 11.26042
## [353] 11.27425 11.28920 11.30521 11.32220 11.34010 11.35883 11.37831 11.39848
## [361] 11.41927 11.44059 11.46237 11.48454 11.50702 11.52975 11.55264 11.57563
## [369] 11.59864 11.62159 11.64441 11.66703 11.69138 11.71920 11.75011 11.78375
## [377] 11.81974 11.85771 11.89729 11.93812 11.97981 12.02199 12.06431 12.10637
## [385] 12.14782 12.18828 12.22738 12.26475 12.30002 12.33282 12.36277 12.38950
## [393] 12.41673 12.44792 12.48232 12.51918 12.55774 12.59727 12.63700 12.67620
## [401] 12.71412 12.74999 12.78308 12.81264 12.83791 12.85815 12.87488 12.89013
## [409] 12.90396 12.91641 12.92754 12.93738 12.94599 12.95341 12.95970 12.96489
## [417] 12.96904 12.97218 12.97438 12.97567 12.97611 12.97574 12.97461 12.97276
## [425] 12.97025 12.96711 12.96340 12.95917 12.95446 12.94932 12.94379 12.93793
## [433] 12.93178 12.92538 12.91880 12.91000 12.89731 12.88128 12.86243 12.84131
## [441] 12.81846 12.79441 12.76971 12.74490 12.72052 12.69710 12.67519 12.65533
## [449] 12.63805 12.62389 12.60955 12.59174 12.57104 12.54806 12.52339 12.49762
## [457] 12.47133 12.44513 12.41960 12.39534 12.37294 12.35299 12.33608 12.32281
## [465] 12.31165 12.30071 12.29000 12.27953 12.26931 12.25937 12.24971 12.24035
## [473] 12.23131 12.22259 12.21421 12.20619 12.19854 12.19128 12.18441 12.17796
## [481] 12.17194 12.16636 12.16123 12.15657 12.15240 12.14873 12.14557 12.14293
## [489] 12.14084 12.13930 12.13834 12.13796 12.13802 12.13841 12.13914 12.14023
## [497] 12.14171 12.14360 12.14593 12.14871 12.15198 12.15574 12.16003 12.16488
## [505] 12.17029 12.17630 12.18293 12.19019 12.19812 12.20674 12.21607 12.22604
## [513] 12.23655 12.24761 12.25919 12.27130 12.28392 12.29705 12.31069 12.32482
## [521] 12.33943 12.35453 12.37010 12.38613 12.40262 12.41956 12.43694 12.45479
## [529] 12.47313 12.49195 12.51127 12.53108 12.55139 12.57219 12.59348 12.61527
## [537] 12.63755 12.66033 12.68360 12.70737 12.73164 12.75641 12.78167 12.80744
## [545] 12.83370 12.86047 12.88773 12.91550 12.94377 12.97254 13.00182 13.03160
## [553] 13.06188 13.09267 13.12396 13.15577 13.18807 13.22089 13.25421 13.28804
## [561] 13.32239 13.35724
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 562)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59625 12.59315 12.59015 12.58723 12.58440 12.58165 12.57898 12.57639
##   [9] 12.57386 12.57141 12.56901 12.56669 12.56441 12.56220 12.56003 12.55791
##  [17] 12.55584 12.55380 12.55181 12.54985 12.54791 12.54601 12.54413 12.54227
##  [25] 12.54042 12.53859 12.53677 12.53496 12.53315 12.53133 12.52949 12.52765
##  [33] 12.52580 12.52395 12.52211 12.52028 12.51848 12.51670 12.51495 12.51324
##  [41] 12.51157 12.50996 12.50839 12.50689 12.50546 12.50410 12.50282 12.50162
##  [49] 12.50052 12.49951 12.49861 12.49781 12.49713 12.49656 12.49613 12.49583
##  [57] 12.49566 12.49557 12.49548 12.49541 12.49535 12.49531 12.49530 12.49532
##  [65] 12.49538 12.49548 12.49563 12.49583 12.49609 12.49641 12.49681 12.49727
##  [73] 12.49781 12.49844 12.49915 12.49997 12.50088 12.50189 12.50301 12.50425
##  [81] 12.50561 12.50710 12.50872 12.51047 12.51236 12.51440 12.51660 12.51895
##  [89] 12.52146 12.52413 12.52699 12.53001 12.53298 12.53567 12.53810 12.54030
##  [97] 12.54230 12.54412 12.54579 12.54735 12.54881 12.55020 12.55155 12.55288
## [105] 12.55423 12.55562 12.55708 12.55862 12.56029 12.56211 12.56410 12.56629
## [113] 12.56871 12.57139 12.57434 12.57761 12.58121 12.58517 12.58953 12.59429
## [121] 12.59945 12.60492 12.61070 12.61676 12.62308 12.62964 12.63641 12.64338
## [129] 12.65053 12.65784 12.66528 12.67283 12.68048 12.68820 12.69597 12.70378
## [137] 12.71159 12.71940 12.72717 12.73489 12.74446 12.75746 12.77334 12.79158
## [145] 12.81164 12.83301 12.85513 12.87749 12.89955 12.92078 12.94064 12.95862
## [153] 12.97417 12.98677 12.99845 13.01153 13.02589 13.04143 13.05803 13.07560
## [161] 13.09402 13.11318 13.13297 13.15328 13.17400 13.19503 13.21625 13.23756
## [169] 13.25885 13.28000 13.30091 13.32146 13.34156 13.36109 13.37993 13.39799
## [177] 13.41515 13.43131 13.44635 13.46016 13.47264 13.48367 13.49315 13.50097
## [185] 13.50702 13.51119 13.51337 13.51344 13.51131 13.50685 13.50010 13.49123
## [193] 13.48041 13.46781 13.45359 13.43792 13.42096 13.40288 13.38385 13.36402
## [201] 13.34358 13.32268 13.30149 13.28017 13.25889 13.23782 13.21713 13.19697
## [209] 13.17752 13.15894 13.13824 13.11283 13.08343 13.05081 13.01570 12.97885
## [217] 12.94100 12.90291 12.86530 12.82894 12.79457 12.76293 12.73476 12.71081
## [225] 12.68845 12.66463 12.63950 12.61320 12.58587 12.55764 12.52867 12.49908
## [233] 12.46901 12.43862 12.40803 12.37739 12.34684 12.31652 12.28657 12.25713
## [241] 12.22833 12.20032 12.17325 12.14724 12.12244 12.09899 12.07703 12.05669
## [249] 12.03813 12.02148 12.00688 11.99447 11.98454 11.97712 11.97193 11.96869
## [257] 11.96714 11.96700 11.96798 11.96982 11.97224 11.97497 11.97773 11.98025
## [265] 11.98225 11.98345 11.98359 11.98239 11.98300 11.98828 11.99746 12.00977
## [273] 12.02447 12.04078 12.05794 12.07520 12.09180 12.10696 12.11994 12.12996
## [281] 12.13628 12.13812 12.13746 12.13676 12.13600 12.13514 12.13418 12.13307
## [289] 12.13179 12.13033 12.12866 12.12674 12.12457 12.12210 12.11932 12.11620
## [297] 12.11272 12.10885 12.10457 12.09985 12.09467 12.08900 12.08282 12.07610
## [305] 12.06882 12.06096 12.05248 12.04336 12.03359 12.02313 12.01038 11.99405
## [313] 11.97455 11.95229 11.92771 11.90121 11.87322 11.84415 11.81441 11.78444
## [321] 11.75464 11.72543 11.69724 11.67047 11.64556 11.62290 11.60294 11.58607
## [329] 11.57273 11.55961 11.54354 11.52511 11.50493 11.48358 11.46166 11.43977
## [337] 11.41849 11.39843 11.38019 11.36434 11.35150 11.34225 11.33719 11.33520
## [345] 11.33475 11.33577 11.33821 11.34202 11.34713 11.35351 11.36108 11.36980
## [353] 11.37962 11.39047 11.40231 11.41507 11.42871 11.44318 11.45840 11.47434
## [361] 11.49093 11.50813 11.52587 11.54410 11.56278 11.58183 11.60122 11.62088
## [369] 11.64076 11.66080 11.68096 11.70117 11.72334 11.74914 11.77818 11.81007
## [377] 11.84441 11.88082 11.91889 11.95824 11.99846 12.03918 12.07999 12.12051
## [385] 12.16034 12.19908 12.23634 12.27174 12.30488 12.33536 12.36279 12.38678
## [393] 12.41013 12.43563 12.46285 12.49137 12.52077 12.55062 12.58050 12.60999
## [401] 12.63867 12.66611 12.69188 12.71557 12.73675 12.75500 12.77143 12.78745
## [409] 12.80301 12.81811 12.83271 12.84680 12.86035 12.87334 12.88574 12.89753
## [417] 12.90870 12.91921 12.92904 12.93817 12.94658 12.95424 12.96114 12.96724
## [425] 12.97253 12.97698 12.98056 12.98327 12.98507 12.98593 12.98585 12.98478
## [433] 12.98272 12.97964 12.97551 12.96947 12.96085 12.94996 12.93712 12.92263
## [441] 12.90680 12.88994 12.87234 12.85433 12.83621 12.81829 12.80087 12.78426
## [449] 12.76877 12.75471 12.73919 12.71958 12.69654 12.67075 12.64288 12.61361
## [457] 12.58361 12.55356 12.52413 12.49598 12.46980 12.44626 12.42603 12.40978
## [465] 12.39526 12.37981 12.36356 12.34660 12.32906 12.31104 12.29265 12.27401
## [473] 12.25523 12.23641 12.21766 12.19911 12.18085 12.16301 12.14568 12.12899
## [481] 12.11304 12.09794 12.08381 12.07076 12.05889 12.04831 12.03915 12.03150
## [489] 12.02549 12.02121 12.01879 12.01833 12.01942 12.02157 12.02472 12.02881
## [497] 12.03381 12.03966 12.04630 12.05370 12.06179 12.07054 12.07989 12.08979
## [505] 12.10019 12.11104 12.12229 12.13390 12.14581 12.15797 12.17033 12.18314
## [513] 12.19667 12.21089 12.22581 12.24140 12.25765 12.27454 12.29207 12.31020
## [521] 12.32894 12.34826 12.36816 12.38861 12.40960 12.43112 12.45315 12.47574
## [529] 12.49891 12.52267 12.54703 12.57197 12.59751 12.62363 12.65035 12.67765
## [537] 12.70554 12.73401 12.76307 12.79272 12.82295 12.85377 12.88517 12.91716
## [545] 12.94973 12.98288 13.01661 13.05093 13.08583 13.12130 13.15736 13.19399
## [553] 13.23121 13.26900 13.30737 13.34632 13.38584 13.42594 13.46661 13.50786
## [561] 13.54969 13.59209
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 562)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.90179 11.90276 11.90376 11.90480 11.90586 11.90693 11.90801 11.90909
##   [9] 11.91017 11.91124 11.91228 11.91329 11.91428 11.91522 11.91611 11.91694
##  [17] 11.91771 11.91841 11.91904 11.91957 11.92002 11.92037 11.92061 11.92074
##  [25] 11.92074 11.92062 11.92037 11.91997 11.91942 11.91872 11.91785 11.91681
##  [33] 11.91560 11.91420 11.91261 11.91081 11.90880 11.90656 11.90411 11.90146
##  [41] 11.89864 11.89566 11.89254 11.88929 11.88594 11.88250 11.87898 11.87541
##  [49] 11.87181 11.86818 11.86455 11.86094 11.85735 11.85382 11.85035 11.84696
##  [57] 11.84368 11.84051 11.83748 11.83460 11.83189 11.82937 11.82705 11.82496
##  [65] 11.82271 11.81994 11.81667 11.81292 11.80873 11.80412 11.79911 11.79372
##  [73] 11.78800 11.78195 11.77560 11.76898 11.76212 11.75504 11.74776 11.74031
##  [81] 11.73272 11.72501 11.71720 11.70933 11.70141 11.69348 11.68555 11.67765
##  [89] 11.66981 11.66206 11.65441 11.64690 11.63954 11.63237 11.62541 11.61868
##  [97] 11.61221 11.60603 11.60016 11.59462 11.58944 11.58465 11.58028 11.57633
## [105] 11.57285 11.56986 11.56738 11.56544 11.56406 11.56326 11.56309 11.56355
## [113] 11.56467 11.56648 11.56901 11.57228 11.57631 11.58113 11.58677 11.59325
## [121] 11.60153 11.61237 11.62556 11.64085 11.65800 11.67678 11.69695 11.71828
## [129] 11.74053 11.76346 11.78683 11.81042 11.83398 11.85728 11.88007 11.90214
## [137] 11.92322 11.94311 11.96154 11.97830 11.99658 12.01929 12.04574 12.07520
## [145] 12.10699 12.14040 12.17473 12.20926 12.24331 12.27617 12.30713 12.33549
## [153] 12.36056 12.38162 12.40101 12.42148 12.44293 12.46526 12.48839 12.51222
## [161] 12.53664 12.56158 12.58692 12.61259 12.63847 12.66449 12.69053 12.71652
## [169] 12.74235 12.76792 12.79316 12.81795 12.84220 12.86583 12.88873 12.91080
## [177] 12.93197 12.95212 12.97117 12.98902 13.00558 13.02075 13.03443 13.04654
## [185] 13.05697 13.06564 13.07244 13.07728 13.08007 13.08131 13.08155 13.08079
## [193] 13.07906 13.07635 13.07268 13.06804 13.06245 13.05592 13.04844 13.04003
## [201] 13.03069 13.02043 13.00926 12.99718 12.98421 12.97034 12.95559 12.93996
## [209] 12.92345 12.90609 12.88487 12.85746 12.82482 12.78790 12.74767 12.70508
## [217] 12.66111 12.61670 12.57281 12.53042 12.49048 12.45394 12.42178 12.39495
## [225] 12.37006 12.34321 12.31459 12.28435 12.25269 12.21977 12.18578 12.15089
## [233] 12.11527 12.07911 12.04258 12.00585 11.96910 11.93252 11.89627 11.86053
## [241] 11.82548 11.79130 11.75816 11.72624 11.69571 11.66676 11.63955 11.61426
## [249] 11.59108 11.57018 11.55173 11.53591 11.52283 11.51231 11.50403 11.49772
## [257] 11.49306 11.48978 11.48758 11.48616 11.48523 11.48449 11.48365 11.48243
## [265] 11.48051 11.47762 11.47345 11.46772 11.46404 11.46568 11.47176 11.48144
## [273] 11.49386 11.50816 11.52348 11.53896 11.55375 11.56698 11.57781 11.58537
## [281] 11.58880 11.58725 11.58270 11.57771 11.57232 11.56652 11.56034 11.55378
## [289] 11.54688 11.53964 11.53207 11.52420 11.51604 11.50761 11.49891 11.48997
## [297] 11.48081 11.47143 11.46185 11.45210 11.44217 11.43210 11.42190 11.41158
## [305] 11.40115 11.39064 11.38005 11.36941 11.35873 11.34803 11.33529 11.31881
## [313] 11.29906 11.27650 11.25160 11.22483 11.19666 11.16754 11.13796 11.10838
## [321] 11.07926 11.05107 11.02428 10.99936 10.97678 10.95699 10.94048 10.92770
## [329] 10.91912 10.91317 10.90794 10.90345 10.89971 10.89672 10.89450 10.89304
## [337] 10.89237 10.89248 10.89339 10.89510 10.89762 10.90096 10.90513 10.91071
## [345] 10.91819 10.92748 10.93848 10.95109 10.96521 10.98074 10.99758 11.01563
## [353] 11.03479 11.05497 11.07606 11.09797 11.12059 11.14383 11.16758 11.19175
## [361] 11.21624 11.24095 11.26577 11.29062 11.31539 11.33997 11.36428 11.38821
## [369] 11.41166 11.43454 11.45674 11.47817 11.50017 11.52402 11.54952 11.57646
## [377] 11.60462 11.63380 11.66378 11.69437 11.72535 11.75650 11.78763 11.81853
## [385] 11.84897 11.87876 11.90769 11.93554 11.96211 11.98719 12.01057 12.03204
## [393] 12.05355 12.07694 12.10183 12.12785 12.15462 12.18178 12.20893 12.23571
## [401] 12.26175 12.28666 12.31007 12.33160 12.35089 12.36755 12.38247 12.39679
## [409] 12.41051 12.42362 12.43612 12.44802 12.45930 12.46997 12.48002 12.48946
## [417] 12.49828 12.50648 12.51405 12.52101 12.52734 12.53304 12.53811 12.54255
## [425] 12.54636 12.54954 12.55208 12.55399 12.55525 12.55587 12.55585 12.55519
## [433] 12.55388 12.55192 12.54931 12.54499 12.53809 12.52896 12.51793 12.50535
## [441] 12.49154 12.47685 12.46161 12.44615 12.43082 12.41594 12.40186 12.38891
## [449] 12.37743 12.36775 12.35776 12.34537 12.33097 12.31493 12.29765 12.27950
## [457] 12.26088 12.24217 12.22375 12.20600 12.18932 12.17408 12.16067 12.14948
## [465] 12.13923 12.12843 12.11715 12.10544 12.09337 12.08100 12.06838 12.05558
## [473] 12.04266 12.02969 12.01671 12.00380 11.99102 11.97841 11.96606 11.95400
## [481] 11.94232 11.93107 11.92030 11.91009 11.90048 11.89155 11.88336 11.87595
## [489] 11.86940 11.86377 11.85911 11.85550 11.85234 11.84909 11.84583 11.84263
## [497] 11.83959 11.83677 11.83428 11.83218 11.83056 11.82950 11.82908 11.82940
## [505] 11.83052 11.83253 11.83552 11.83956 11.84474 11.85114 11.85885 11.86745
## [513] 11.87651 11.88605 11.89608 11.90663 11.91772 11.92937 11.94159 11.95442
## [521] 11.96787 11.98196 11.99671 12.01215 12.02829 12.04515 12.06276 12.08105
## [529] 12.09997 12.11952 12.13969 12.16048 12.18190 12.20396 12.22664 12.24995
## [537] 12.27389 12.29846 12.32367 12.34952 12.37599 12.40311 12.43086 12.45926
## [545] 12.48829 12.51796 12.54828 12.57924 12.61084 12.64309 12.67598 12.70952
## [553] 12.74371 12.77855 12.81404 12.85019 12.88698 12.92443 12.96253 13.00129
## [561] 13.04071 13.08078
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")